package com.xiaohudie.Lambda;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author xiao hu die
 * @Date 2022/2/22 14:59
 * @Version 1.0
 */
public class Test01 {
 public static void main(String[] args) {
  //test() ;
  //test2();
  // test3();
  // test4();
     // test5();
   test6();
 }

    private static void test6() {
        Booke booke1 = new Booke("1", 1, 12.0f);
        Booke booke2 = new Booke("2", 2, 22.0f);
        Booke booke3 = new Booke("3", 3, 32.0f);
        Booke booke4 = new Booke("4", 4, 42.0f);
        Booke booke5 = new Booke("5", 5, 52.0f);
        Booke booke6 = new Booke("5", 5, 52.0f);
        List<Booke> list =new ArrayList<>();
        list.add(booke1);
        list.add(booke2);
        list.add(booke3);
        list.add(booke4);
        list.add(booke5);
        list.add(booke6);
        Integer reduce = list.stream().map(x -> x.getId()).reduce(0, (x, y) -> x + y);
        System.out.println(reduce);
    }

    private static void test5() {
  /**实现group by*/
  Booke booke1 = new Booke("1", 1, 12.0f);
  Booke booke2 = new Booke("2", 2, 22.0f);
  Booke booke3 = new Booke("3", 3, 32.0f);
  Booke booke4 = new Booke("4", 4, 42.0f);
  Booke booke5 = new Booke("5", 5, 52.0f);
  Booke booke6 = new Booke("5", 5, 52.0f);
  List<Booke> list =new ArrayList<>();
  list.add(booke1);
  list.add(booke2);
  list.add(booke3);
  list.add(booke4);
  list.add(booke5);
  list.add(booke6);
  Map<Integer, List<Booke>> collect = list.stream().collect(Collectors.groupingBy(Booke::getId));
 /* Set<Integer> integers = collect.keySet();
  for (Integer integer : integers) {
   System.out.println("Key是:"+integer +"Value是:" +collect.get(integer));
  }*/
 /* Iterator<Map.Entry<Integer, List<Booke>>> iterator = collect.entrySet().iterator();
  while (iterator.hasNext()){
   Map.Entry<Integer, List<Booke>> next = iterator.next();
   System.out.println("Key是:"+next.getKey() +"Value是:" +next.getValue());
  }*/
  for (Map.Entry<Integer, List<Booke>> integerListEntry : collect.entrySet()) {
   System.out.println("Key是:"+integerListEntry.getKey()+"Value是:"+integerListEntry.getValue());
  }
 }

 private static void test4() {
  Booke booke1 = new Booke("1", 1, 12.0f);
  Booke booke2 = new Booke("2", 2, 22.0f);
  Booke booke3 = new Booke("3", 3, 32.0f);
  Booke booke4 = new Booke("4", 4, 42.0f);
  Booke booke5 = new Booke("5", 5, 52.0f);
  Booke booke6 = new Booke("5", 5, 52.0f);
  List<Booke> list =new ArrayList<>();
  list.add(booke1);
  list.add(booke2);
  list.add(booke3);
  list.add(booke4);
  list.add(booke5);
  list.add(booke6);
  List<Float> collect = list.stream().map(x -> x.getPrice() * 10).collect(Collectors.toList());
  for (Float aFloat : collect) {
   System.out.println(aFloat);
  }
 }

 private static void test3() {
  Booke booke1 = new Booke("1", 1, 12.0f);
  Booke booke2 = new Booke("2", 2, 22.0f);
  Booke booke3 = new Booke("3", 3, 32.0f);
  Booke booke4 = new Booke("4", 4, 42.0f);
  Booke booke5 = new Booke("5", 5, 52.0f);
  Booke booke6 = new Booke("5", 5, 52.0f);
  List<Booke> list =new ArrayList<>();
  list.add(booke1);
  list.add(booke2);
  list.add(booke3);
  list.add(booke4);
  list.add(booke5);
  list.add(booke6);
  List<String> collect = list.stream().map(x -> x.getName()).distinct().collect(Collectors.toList());
  for (String s : collect) {
   System.out.println(s);
  }
 }

 private static void test2() {
  Booke booke1 = new Booke("1", 1, 12.0f);
  Booke booke2 = new Booke("2", 2, 22.0f);
  Booke booke3 = new Booke("3", 3, 32.0f);
  Booke booke4 = new Booke("4", 4, 42.0f);
  Booke booke5 = new Booke("5", 5, 52.0f);
  Booke booke6 = new Booke("5", 5, 52.0f);
  List<Booke> list =new ArrayList<>();
  list.add(booke1);
  list.add(booke2);
  list.add(booke3);
  list.add(booke4);
  list.add(booke5);
  list.add(booke6);
  List<Booke> collect = list.stream().distinct().sorted((x, y) -> y.getId() - x.getId()).limit(2).collect(Collectors.toList());
  for (Booke booke : collect) {
   System.out.println(booke);
  }
 }

 private static List<People> getList(){
   Booke booke1 = new Booke("1", 1, 12.0f);
   Booke booke2 = new Booke("2", 2, 22.0f);
   Booke booke3 = new Booke("3", 3, 32.0f);
   Booke booke4 = new Booke("4", 4, 42.0f);
   Booke booke5 = new Booke("5", 5, 52.0f);
   Booke booke6 = new Booke("5", 5, 52.0f);
   List<People> list4 =new ArrayList<>();
   List<Booke> list =new ArrayList<>();
   List<Booke> list2 =new ArrayList<>();
   List<Booke> list3 =new ArrayList<>();
   list.add(booke1);
   list.add(booke2);
   list.add(booke3);
   list.add(booke4);
   list.add(booke5);
   list.add(booke6);
   list2.add(booke1);
   list2.add(booke3);
   list2.add(booke5);
   list3.add(booke2);
   list3.add(booke5);
   list3.add(booke6);
   People people1 = new People(1, "于涛", list);
   People people2 = new People(2, "袁伟", list2);
   People people3 = new People(3, "签名", list3);
   list4.add(people1);
   list4.add(people2);
   list4.add(people3);
   return list4;
  }
 private static void test() {
  List<String> list =new ArrayList<>();
  list.add("涨水");
  list.add("程璐");
  list.add("小虎");
  list.add("Gala");
  list.add("小明");
  List<String> list1 = list.stream().
          /**中间操作过滤*/
                  filter(x -> x.startsWith("小"))
          /**终止操作收集*/
          .collect(Collectors.toList());
  for (String s : list1) {
   System.out.println(s);
  }
 }

}
